package com.tedu.day08springmvc.controller;

import com.tedu.day08springmvc.mapper.UserMapper;
import com.tedu.day08springmvc.pojo.dto.UserDto;
import com.tedu.day08springmvc.pojo.entity.User;
import com.tedu.day08springmvc.pojo.vo.UserVO;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;

@RestController//使用该注解后所有方法都不需要加入ResposeBody注解
//@Controller
public class UserController {
    @Autowired
    private UserMapper userMapper;

//    @RequestMapping("/users/reg")
    @PostMapping("/users/reg")
//    @ResponseBody
    public String reg(UserDto userDto) {
        User user = new User();
        System.out.println(user);
        BeanUtils.copyProperties(userDto,user);
        System.out.println(user);
        userMapper.insertUser(user);
        return "注册成功";
    }
//    @RequestMapping("/users/delete")
    @DeleteMapping("/users/delete/{id}")
//    @ResponseBody
    public String delete(@PathVariable int id){
        int num = userMapper.deleteUser(id);
        return num>0? "删除成功":"删除失败";
    }
//    @RequestMapping("/users/update")
    @PutMapping("/users/update")
//    @ResponseBody
    public String update(UserDto userDto) {
        User user =new User();
        BeanUtils.copyProperties(userDto,user);
        int num = userMapper.updateUser(user);
        return num>0? "修改成功":"修改成功";
    }
//    @RequestMapping(value = "/users/find",method = RequestMethod.GET)
    @GetMapping("/users/find")
//    @ResponseBody
    public List<UserVO> findAll(){
        List<UserVO> list = userMapper.findAll();
        return list;
    }

    public UserVO findById(int id){
        return null;
    }
}
